package com.neu.carbon.mes.service.impl;

import java.util.List;

import com.neu.carbon.mes.mapper.JobMapper;
import com.neu.carbon.mes.mapper.PlanMapper;
import com.neu.carbon.wms.domain.WmsMaterialInfo;
import com.neu.carbon.wms.mapper.WmsMaterialInfoMapper;
import com.neu.common.enums.SerialNoPrefix;
import com.neu.smty.utils.CodeUtil;
import org.apache.el.stream.StreamELResolverImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neu.carbon.mes.mapper.MesProductFinishMapper;
import com.neu.carbon.mes.domain.MesProductFinish;
import com.neu.carbon.mes.service.IMesProductFinishService;

/**
 * 生产完工单Service业务层处理
 * 
 * @author neuedu
 * @date 2024-11-25
 */
@Service
public class MesProductFinishServiceImpl implements IMesProductFinishService 
{
    @Autowired
    private MesProductFinishMapper mesProductFinishMapper;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private WmsMaterialInfoMapper wmsMaterialInfoMapper;

    /**
     * 查询生产完工单
     * 
     * @param id 生产完工单ID
     * @return 生产完工单
     */
    @Override
    public MesProductFinish selectMesProductFinishById(Long id)
    {
        MesProductFinish mesProductFinish = mesProductFinishMapper.selectMesProductFinishById(id);
        Long planId = mesProductFinish.getPlanId();
        Long jobId = mesProductFinish.getJobId();
        Long materialId = mesProductFinish.getMaterialId();

        if(planId!=null){
            String no = planMapper.selectNoById(planId);
            mesProductFinish.setPlanNo(no);
        }
        if(jobId!=null){
            String no = jobMapper.selectNoById(jobId);
            mesProductFinish.setJobNo(no);
        }
        if(materialId!=null){
            WmsMaterialInfo materialInfo = wmsMaterialInfoMapper.selectWmsMaterialInfoById(materialId);
            String name = materialInfo.getName();
            String specification = materialInfo.getSpecification();
            String model = materialInfo.getModel();
            String unit = materialInfo.getUnit();

            if(name!=null){
                mesProductFinish.setMaterialName(name);
            }
            if(specification!=null){
                mesProductFinish.setSpecification(specification);
            }
            if(model!=null){
                mesProductFinish.setModel(model);
            }
            if(unit!=null){
                mesProductFinish.setUnit(unit);
            }
        }
        return mesProductFinish;

    }

    /**
     * 查询生产完工单列表
     * 
     * @param mesProductFinish 生产完工单
     * @return 生产完工单
     */
    @Override
    public List<MesProductFinish> selectMesProductFinishList(MesProductFinish mesProductFinish)
    {
        List<MesProductFinish> list = mesProductFinishMapper.selectMesProductFinishList(mesProductFinish);
        if(list!=null){
            for (MesProductFinish productFinish : list) {
                Long planId = productFinish.getPlanId();
                Long jobId = productFinish.getJobId();
                Long materialId = productFinish.getMaterialId();

                if(planId!=null){
                    String no = planMapper.selectNoById(planId);
                    productFinish.setPlanNo(no);
                }
                if(jobId!=null){
                    String no = jobMapper.selectNoById(jobId);
                    productFinish.setJobNo(no);
                }
                if(materialId!=null){
                    WmsMaterialInfo materialInfo = wmsMaterialInfoMapper.selectWmsMaterialInfoById(materialId);
                    String name = materialInfo.getName();
                    String specification = materialInfo.getSpecification();
                    String model = materialInfo.getModel();
                    String unit = materialInfo.getUnit();

                    if(name!=null){
                        productFinish.setMaterialName(name);
                    }
                    if(specification!=null){
                        productFinish.setSpecification(specification);
                    }
                    if(model!=null){
                        productFinish.setModel(model);
                    }
                    if(unit!=null){
                        productFinish.setUnit(unit);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 新增生产完工单
     * 
     * @param mesProductFinish 生产完工单
     * @return 结果
     */
    @Override
    public int insertMesProductFinish(MesProductFinish mesProductFinish)
    {
        mesProductFinish.setSerialNo(CodeUtil.getSerialNo(SerialNoPrefix.SCWG));
        mesProductFinish.setBatchNo(CodeUtil.getSerialNo(SerialNoPrefix.CPB));
        return mesProductFinishMapper.insertMesProductFinish(mesProductFinish);
    }

    /**
     * 修改生产完工单
     * 
     * @param mesProductFinish 生产完工单
     * @return 结果
     */
    @Override
    public int updateMesProductFinish(MesProductFinish mesProductFinish)
    {
        return mesProductFinishMapper.updateMesProductFinish(mesProductFinish);
    }

    /**
     * 批量删除生产完工单
     * 
     * @param ids 需要删除的生产完工单ID
     * @return 结果
     */
    @Override
    public int deleteMesProductFinishByIds(Long[] ids)
    {
        return mesProductFinishMapper.deleteMesProductFinishByIds(ids);
    }

    /**
     * 删除生产完工单信息
     * 
     * @param id 生产完工单ID
     * @return 结果
     */
    @Override
    public int deleteMesProductFinishById(Long id)
    {
        return mesProductFinishMapper.deleteMesProductFinishById(id);
    }
}
